<template>
  <div id="battery">
    <div class="search_box">
      <div class="search_input">
        <el-select v-model="modelid" :placeholder="$t('battery.model')">
          <el-option v-for="item in model" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select v-model="portid" :placeholder="$t('battery.port')">
          <el-option v-for="item in port" :key="item.id" :label="item.label" :value="item.id" />
        </el-select>
        <el-select v-model="statusid" :placeholder="$t('battery.status')">
          <el-option v-for="item in status" :key="item.id" :label="item.label" :value="item.id" />
        </el-select>
      </div>
      <div class="search_btn">
        <el-button type="primary" icon="el-icon-search">{{ $t('ele.btn_search') }}</el-button>
        <el-button icon="el-icon-refresh-right">{{ $t('ele.btn_reset') }}</el-button>
      </div>
    </div>
    <el-button
      class="add_btn"
      type="primary"
      icon="el-icon-plus"
      @click="createActive = true"
    >{{ $t('ele.btn_add') }}</el-button>
    <el-button
      class="add_btn"
      type="primary"
      icon="el-icon-plus"
      @click="createActive = true"
    >{{ $t('ele.btn_upload') }}</el-button>
    <el-table :data="list" border>
      <!-- <el-table-column label="ID" width="70" /> -->
      <el-table-column prop="serial" :label="$t('battery.serial')" width="150" align="center" />
      <el-table-column prop="model" :label="$t('battery.model')" width="120" align="center" />
      <el-table-column :label="$t('battery.picture')" width="150" align="center">
        <template>
          <el-image
            style="width: 50px; "
            src="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
            :preview-src-list="['https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg']"
          />
        </template>
      </el-table-column>
      <el-table-column prop="port" :label="$t('battery.port')" width="100" align="center" />
      <el-table-column :label="$t('battery.status')" width="150" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">{{ $t('battery.assigned') }}</el-tag>
          <el-tag v-else type="info">{{ $t('battery.undistributed') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" :label="$t('battery.description')" width="300" show-overflow-tooltip />
      <el-table-column prop="" :label="$t('ele.controls')">
        <!-- <el-button type="text" @click="toinfo">查看</el-button> -->
        <el-button type="text" @click="createActive = true">{{ $t('ele.btn_edit') }}</el-button>
        <el-button type="text" @click="torelieve">{{ $t('ele.btn_relieve') }}</el-button>
        <el-button type="text" @click="todistribution">{{ $t('ele.btn_distribution') }}</el-button>
      </el-table-column>
    </el-table>
    <el-drawer
      title="录入电柜信息"
      size="40%"
      :visible.sync="createActive"
      :before-close="handleClose"
    >
      <div class="demo-drawer__content">
        <el-form ref="ruleForm" :model="form" label-width="90px" :rules="rules">
          <el-form-item label="序列号" prop="serial">
            <el-input v-model="form.serial" />
          </el-form-item>
          <el-form-item label="型号" prop="model">
            <el-input v-model="form.model" />
          </el-form-item>
          <el-form-item label="接口" prop="port">

            <el-radio v-model="form.port" label="1">3*3</el-radio>
            <el-radio v-model="form.port" label="2">4*4</el-radio>
          </el-form-item>
          <el-form-item label="图片" prop="picture">
            <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
              list-type="picture-card"
            >
              <i class="el-icon-plus" />
            </el-upload>
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input
              v-model="form.name"
              type="textarea"
              style="width: 400px;"
              :rows="4"
            />
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </div>
    </el-drawer>
    <el-drawer
      title="分配电柜"
      size="40%"
      :visible.sync="distributionActive"
      :before-close="closeDis"
    >
      <div class="demo-drawer__content" style="padding: 20px 30px; margin-bottom: 50px;">
        <el-descriptions title="电柜信息" direction="vertical" :column="3" border>
          <el-descriptions-item label="序列号">199</el-descriptions-item>
          <el-descriptions-item label="型号">60v</el-descriptions-item>
          <el-descriptions-item label="插口数" :span="2">9</el-descriptions-item>
          <el-descriptions-item label="选择代理商">
            <el-button type="primary" size="small" @click="chooseatt">选择代理商</el-button>
          </el-descriptions-item>
        </el-descriptions>

        <div v-if="dislist[0]" style="margin-top: 50px;">
          <el-descriptions title="代理商信息" direction="vertical" :column="3" border style="margin-bottom: 30px;">
            <el-descriptions-item label="名称">{{ dislist[0].name }}</el-descriptions-item>
            <el-descriptions-item label="地址">{{ dislist[0].address }}</el-descriptions-item>
            <el-descriptions-item label="联系方式">{{ dislist[0].mobile }}</el-descriptions-item>
          </el-descriptions>
          <el-form ref="atform" :model="disform" :rules="disrule">
            <el-form-item label="名称" prop="disname">
              <el-input v-model="disform.disname" />
            </el-form-item>
            <el-form-item label="地址" prop="disaddress">
              <el-input
                v-model="disform.address"
                type="textarea"
                style="width: 400px;"
                :rows="4"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="demo-drawer__footer">
          <el-button @click="closeDis">取 消</el-button>
          <el-button type="primary" @click="submitAt">确 定</el-button>
        </div>
      </div>
    </el-drawer>

    <ChooseAt :show="show" @close="closeAt" @choose="choose" />
  </div>
</template>
<script>
import ChooseAt from '../distribution/chooseAt.vue'
export default {
  name: 'Battery',
  components: { ChooseAt },
  data() {
    return {
      value: '',
      model: [
        { label: '60v', value: '60v' },
        { label: '48v', value: '48v' },
        { label: '60max', value: '60max' },
        { label: '48max', value: '48max' },
        { label: '48c', value: '48c' },
        { label: '60c', value: '60c' }
      ],
      modelid: '',
      port: [
        { id: 1, label: '3*3' },
        { id: 2, label: '4*4' },
        { id: 3, label: '5*5' }
      ],
      portid: '',
      status: [
        { id: 1, label: '已分配' },
        { id: 2, label: '未分配' }
      ],
      statusid: '',
      list: [{
        serial: '199',
        port: 9,
        model: '60V',
        description: '9个充电口,只支持60V电瓶更换',
        picture: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
        status: 1
      }, {
        serial: '199',
        port: 9,
        model: '60V',
        description: '9个充电口,只支持60V电瓶更换',
        picture: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
        status: 2
      }],
      createActive: false,
      distributionActive: false,
      show: false,
      form: {
        serial: '',
        port: '',
        model: '',
        picture: ''
      },
      rules: {
        serial: [
          { required: true, message: '请填写序列号', trigger: 'change' }
        ],
        model: [
          { required: true, message: '请选择型号', trigger: 'change' }
        ],
        port: [
          { required: true, message: '请选择槽数', trigger: 'change' }
        ],
        picture: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入', trigger: 'change' }
        ]
      },
      dislist: [],
      disform: {
        disname: '',
        disaddress: ''
      },
      disrule: {
        disname: [
          { required: true, message: '请填写电柜名称', trigger: 'change' }
        ],
        disaddress: [
          { required: true, message: '请填写电柜地址', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    langSize(key) {
      this.form.specification[0] = key
    },
    torelieve() {
      this.$confirm('确定解除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '已删除'
        })
      })
    },
    toinfo() {
      this.$router.push({ path: '/batteryinfo' })
    },
    todistribution() {
      this.distributionActive = true
    },
    closeDis() {
      this.distributionActive = false
      this.dislist = []
    },
    chooseatt() {
      this.show = true
    },
    closeAt() {
      this.show = false
    },
    choose(item) {
      this.dislist[0] = item
      this.closeAt()
    },
    handleClose() {
      this.form = {
        serial: '',
        port: '',
        model: '',
        picture: ''
      }
      this.createActive = false
    },
    submit() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          alert('submit!')
        } else {
          return false
        }
      })
    },
    submitAt() {
      if (this.dislist[0]) {
        this.$refs['atform'].validate(valid => {
          if (valid) {
            alert('submit!')
          } else {
            return false
          }
        })
      } else {
        this.$message.info('请选择代理商')
      }
    }
  }
}
</script>
<style lang='scss' scoped>

  .el-drawer {
    position: relative;
    .el-input {
      width: 400px;
    }
    .demo-drawer__footer {
      position: absolute;
      bottom:30px;
      right:30px;
    }
    .el-tag{
      margin-right: 20px;
      cursor: pointer;
    }
  }

</style>
